package org.hackystat.devcathlon.engine.user;

import java.util.List;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 * Test UserManager DB class.
 * 
 * @author Anthony Du
 *
 */
public class TestUserManager {

  private static final String firstName = "Anthony";
  private static final String lastName = "Du";
  private static final String email = "anthony@gmail.com";
  private static final String password = email;

  /**
   * Test adding/loading/deleting of a User.
   */
  @Test
  public void testUserManager() {
    User user = UserManager.getInstance().addUser(firstName, lastName, email, password);
    User selectedUser = UserManager.getInstance().getUser(email);
    // test to see if the user added is the same as the user selected.
    assertEquals("should be the same", selectedUser, user);
    // clean up
    UserManager.getInstance().deleteUser(email);
    // test to see if the user deleted is truly gone.
    assertNull("should be null", UserManager.getInstance().getUser(email));
  }

  /**
   * Test getUsers.
   */
  @Test
  public void testGetUsers() {
    // Test to see if the list of users is empty.
    List usersList = UserManager.getInstance().getUsers();
    assertTrue("users list should be empty", usersList.isEmpty());
    User user1 = UserManager.getInstance().addUser(firstName, lastName, email, password);
    User user2 = UserManager.getInstance().addUser(firstName, lastName, email + ".net", password);
    // Test to see if only two users reside in the query.
    usersList = UserManager.getInstance().getUsers();
    int numUsers = usersList.size();
    assertEquals("should have 2 users", 2, numUsers);
    // Test if the users were the original ones added.
    User selectedUser = UserManager.getInstance().getUser(email);
    assertEquals("should be user 1", user1, selectedUser);
    selectedUser = UserManager.getInstance().getUser(email + ".net");
    assertEquals("should be user 2", user2, selectedUser);
    UserManager.getInstance().deleteUser(email);
    UserManager.getInstance().deleteUser(email + ".net");
    usersList = UserManager.getInstance().getUsers();
    assertTrue("users list should be empty", usersList.isEmpty());
  }
}
